//
//  MyNickNameController.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/6.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "MyNickNameController.h"
#import "FbeeCommitBtn.h"
#import "FbeeInputView.h"

@interface MyNickNameController ()
{
    NSString *nickname;
    
    UIScrollView *mainView;
    UIButton *backBtn;
    FbeeInputView *nicknameF;
    FbeeCommitBtn *commit;
    
    BOOL isEx;
}
@end

@implementation MyNickNameController


-(void)dealloc
{
    NSLog(@"MyNickNameController dealloc");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(instancetype)initWithEx
{
    self = [super init];
    
    if (self) {
        isEx = YES;
    }
    
    return self;
}

-(void)viewDidAppear:(BOOL)animated
{
    [nicknameF becomeFirstResponder];
    
    if (isEx) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationView.hidden = YES;
    
    
    
    // 添加观察
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [self buildView];
    
    if (isEx) {
        nicknameF.textF.text = @"";
        [self hiddeBack];
        backBtn.hidden = YES;
    }
    
}

-(void)buildView
{
    
    UILabel *infoL = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.addFont([UIFont systemFontOfSize:14.0f]).addTextColor(FBEE_COLOR_INFO);
        make.numberOfLines = 2;
        make.setFrame(CGRectMake(18, self.largeTitle.bottom + 5, IPHONEWIDTH - 18*2, 40));
        [self.view addSubview:make];
    }];
    [infoL setText:NSLocalizedString(@"textNicknameDesc", nil) lineSpacing:4];
    
    mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT)];
    mainView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mainView];
    
    nicknameF = [[FbeeInputView alloc] initWithText:NSLocalizedString(@"textInputNickname", nil)];
    nicknameF.centerX = IPHONEWIDTH/2;
    nicknameF.top = 238.5;
    nicknameF.textF.text = [UserInfoManager sharedManager].nickName;
    [mainView addSubview:nicknameF];
    
    commit = [[FbeeCommitBtn alloc] initWithTitle:NSLocalizedString(@"mConfirm", nil)];
    commit.centerX = IPHONEWIDTH/2;
    commit.top = nicknameF.bottom + 65;
    @weakify(self)
    [commit onClick:^{
        @strongify(self)
        [self setNickName];
    }];
    [mainView addSubview:commit];
    
    
    [self.largeTitle setTitle:NSLocalizedString(@"tNavUCenterNickname", nil)];
    
    backBtn = [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, StatusHeight, 60, 44));
        make.addImage([UIImage imageNamed:@"nav_back"],UIControlStateNormal);
        make.addTarget(self,@selector(navback));
        [self.view addSubview:make];
    }];
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(13.5, 16, 13.5, 27);
    
    @weakify(nicknameF);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        @strongify(nicknameF);
        [nicknameF resignFirstResponder];
    }];
    [mainView addGestureRecognizer:tap];
    
    [self bringTitleToFront];
    
    if ([KeyBoardManager sharedManager].keyBoardShow) {
        float sub = commit.bottom - (IPHONEHEIGHT - [KeyBoardManager sharedManager].keyHeight);
        if (sub > 0) {
            [mainView setContentOffset:CGPointMake(0, sub) animated:YES];
        }
        
    }
}

#pragma mark - key

- (void)keyboardWillShow:(NSNotification *)notification {
    float sub = commit.bottom - (IPHONEHEIGHT - [KeyBoardManager sharedManager].keyHeight);
    if (sub > 0) {
        [mainView setContentOffset:CGPointMake(0, sub) animated:YES];
    }
}

- (void)keyboardWillHide:(NSNotification *)notification {
    if (mainView.contentOffset.y>0) {
        [mainView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}

#pragma mark - data

-(void)setNickName
{
    if ([NSString isEmptyString:[nicknameF getText]]) {
        [NoticePopHelper showNoticeText:NSLocalizedString(@"textInputNickname", nil)];
        return;
    }
    
    [self post:NicknameModify_URL param:@{@"uid":[UserInfoManager sharedManager].userId,@"nickname":[nicknameF getText]} code:NicknameModify_URL_Code];
}

#pragma mark - requst

-(void)requestSuccess:(id)data code:(NSInteger)code
{
    [UserInfoManager sharedManager].nickName = [nicknameF getText];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    [self navback];
}

-(void)requestLogicError:(id)data code:(NSInteger)code
{
    [super requestLogicError:data code:code];
}

-(void)requestFailure:(NSError *)error code:(NSInteger)code
{
    
}


@end
